package com.zh.note.demo.javaSE;

/**
 * @Author: zhanghuan
 * @date: 2023/9/5 10:46
 * @description: create it
 */
public class BitDemo {
    public static void main(String[] args) {

        int M = 254;

        int mant = M & 0x0f; //mat是M的低四位
        int exp = (M >> 4) & 0x7; //m的高5～7位

        String s = Integer.toBinaryString(M);
        System.out.println("二进制：" + s);
        System.out.println("获取低4位:" + mant);
        System.out.println("获取高5～7位: " + exp);

        String format = String.format("%8s", s);
        System.out.println(format);
        String mantStr = format.substring(format.length() - 4);
        int mant2 = Integer.parseInt(mantStr, 2);
        String substring = format.substring(1, 4);
        int exp2 = Integer.parseInt(substring, 2);

        System.out.println(mant2);
        System.out.println(exp2);
    }
}
